/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：AuthenticationFace.java
 * History:
 *       2012-1-18: Initially created, tangkf.
 */
package com.xteam.purview.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.xteam.purview.entities.PurviewFunNode;
import com.xteam.purview.entities.PurviewRoleFunDatarule;
import com.xteam.purview.entities.PurviewType;
import com.xteam.purview.entities.PurviewUser;
import com.xteam.purview.entities.PurviewUserFunDatarule;
import com.xteam.purview.entities.PurviewUserRole;
import com.xteam.purview.service.AbsFace;
import com.xteam.purview.service.IAuthenticationFace;
import com.xteam.utils.TreeNodeUtils;

/**
 * 认证接口规范实现 用于实现权限认证，鉴权的相关规范
 * @author tangkf ---2012-1-18
 */
public class AuthenticationFace extends AbsFace<PurviewUser> implements IAuthenticationFace {
	
	private AuthenticationFace() {
	};

	private static AuthenticationFace	instance	= new AuthenticationFace();

	public static AuthenticationFace getInstance() {
		return instance;
	}

	AuthorizationFace	aface	= AuthorizationFace.getInstance();

	/**
	 * @see com.xteam.purview.service.IAuthenticationFace#getPurviewList(java.lang.String, java.lang.String)
	 */
	public List<PurviewType> getPurviewList(String userId, String funNodeId) {
		List<PurviewType> rtp = null;

		return rtp;
	}

	/**
	 * 获取用户具有的权限树
	 * @author tangkf ---2012-2-9
	 * @param userId
	 */
	public JSONArray getFunNodeTreeByUser(String userId) {
		PurviewFunNodeFace fnface = PurviewFunNodeFace.getInstance();
		// 超级用户
		if (PurviewUserFace.superMan.equals(userId)) {
			List<PurviewFunNode> pfns = fnface.getAllFunNodes();
			return TreeNodeUtils.getJSONArrayTreeNode(pfns, "0");
		}

		// 记录具有权限的功能节点
		Map<String, String> funIds = new HashMap<String, String>();
		// 用户的角色关系表
		List<PurviewUserRole> purs = aface.getUserRoleRelation(userId);
		for (PurviewUserRole pur : purs) {
			// 角色的功能关系表
			List<PurviewRoleFunDatarule> prfs = aface.getRoleFunNodeRelation(pur.getRoleId());
			for (PurviewRoleFunDatarule prf : prfs) {
				funIds.put(prf.getFunId(), prf.getFunId());
			}
		}
		// 用户的功能关系表
		List<PurviewUserFunDatarule> pufs = aface.getUserFunNodeRelation(userId);
		for (PurviewUserFunDatarule puf : pufs) {
			funIds.put(puf.getFunId(), puf.getFunId());
		}
		List<PurviewFunNode> list = fnface.getFunNodeList(funIds.values());
		if(list==null) list	= new ArrayList<PurviewFunNode>();
		JSONArray jsa = TreeNodeUtils.getJSONArrayTreeNode(list, "0");
		return jsa;
	}

	/**
	 * @see com.xteam.purview.service.IAuthenticationFace#getFunNodeByUser(java.lang.String)
	 */
	public Map<String, PurviewFunNode> getFunNodeByUser(String userId) {
		Map<String, PurviewFunNode> fnmap = new HashMap<String, PurviewFunNode>();

		PurviewFunNodeFace fnface = PurviewFunNodeFace.getInstance();

		if (PurviewUserFace.superMan.equals(userId)) {
			List<PurviewFunNode> pfns = fnface.getAllFunNodes();
			for (PurviewFunNode fn : pfns) {
				fnmap.put(fn.getFunId(), fn);
			}
			return fnmap;
		}

		// 用户的角色关系表
		List<PurviewUserRole> purs = aface.getUserRoleRelation(userId);
		for (PurviewUserRole pur : purs) {
			// 角色的功能关系表
			List<PurviewRoleFunDatarule> prfs = aface.getRoleFunNodeRelation(pur.getRoleId());
			for (PurviewRoleFunDatarule prf : prfs) {
				PurviewFunNode fn = fnface.getObject(PurviewFunNode.class, prf.getFunId());
				fnmap.put(fn.getFunId(), fn);
			}
		}

		// 用户的功能关系表
		List<PurviewUserFunDatarule> pufs = aface.getUserFunNodeRelation(userId);
		for (PurviewUserFunDatarule puf : pufs) {
			PurviewFunNode fn = fnface.getObject(PurviewFunNode.class, puf.getFunId());
			fnmap.put(fn.getFunId(), fn);
		}
		return fnmap;
	}

	/**
	 * @see com.xteam.purview.service.IAuthenticationFace#hasPurview(java.lang.String, java.lang.String)
	 */
	public boolean hasPurview(String userId, String funNodeId) {
		return false;
	}

	/**
	 * @see com.xteam.purview.service.IAuthenticationFace#hasPurview(java.lang.String, java.lang.String, java.lang.String)
	 */
	public boolean hasPurview(String userId, String funNodeId, String typeId) {
		return false;
	}

	/**
	 * @see com.xteam.purview.service.IAuthenticationFace#hasLoginPurview(java.lang.String)
	 */
	public boolean hasLoginPurview(String userId) {
		return false;
	}
}